<?php

class Tabela
{
	static public function getTabela($head, $body, $json = "{}")
	{
		$options = json_decode($json);
		$modulo  = "m={$_GET["m"]}&";
		
		foreach ($head as $key => $val)
		{
			$fields[] = $val;
			
			$head_html .= "<th>$key</th>";
		}

		if($options->acoes !== false)
		{
			$head_html .= "<th colspan=\"2\"></th>";
		}
		
		foreach ($body as $val)
		{
			$cod = null;
			
			$body_html .= "<tr>";
			
			foreach ($val as $key => $v)
			{	
				!$cod?$cod=$v:"";
			
				if(in_array($key, $fields))
				{
					if($options->page)
					{
						$v = "<a href=\"index.php?p=$options->page&a=$options->action&cod=$cod\">$v</a>";
					}
					
					$body_html .= "<td>$v</td>";
				}					
			}
			
			$alterar_html = "<a href=\"?{$modulo}p={$_GET["p"]}&a=alterar&cod=$cod\">alterar</a>";
			$excluir_html = "<a href=\"?{$modulo}p={$_GET["p"]}&a=excluir&cod=$cod\">excluir</a>";
			
			if($options->acoes !== false)
			{
				$body_html .= "<td>$alterar_html</td><td>$excluir_html</td>";
			}
			
			$body_html .= "</tr>";
			
		}
		
		$html = 
		"
		<table class=\"grid\" width='100%'>
			<thead>
				<tr>
					$head_html
				</tr>
			</thead>
			<tbody>
				$body_html
			</tbody>
		</table>
		<div id=\"counter\"></div>
		";
		
		return $html;
	}
}

?>